package com.hz.zbl.mapper;

import com.hz.zbl.model.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users")
    List<UserEntity> allUsers();

    @Insert("INSERT INTO users VALUES (null, #{username}, #{realname}, #{student_id}, null, #{pwd_salt}, #{pwd}, #{pwd_valid_date}, #{user_type}, #{pwd_error_count}, #{class_id}, #{grade_id})")
    int addStudent(UserEntity userEntity);

    @Insert("INSERT INTO users VALUES (null, #{username}, #{realname}, null, #{teacher_id}, #{pwd_salt}, #{pwd}, #{pwd_valid_date}, #{user_type}, #{pwd_error_count}, #{class_id}, #{grade_id})")
    int addTeacher(UserEntity userEntity);

    @Insert("INSERT INTO users VALUES (null, #{username}, #{realname}, null, null, #{pwd_salt}, #{pwd}, #{pwd_valid_date}, #{user_type}, #{pwd_error_count}, #{class_id}, #{grade_id})")
    int addManager(UserEntity userEntity);

    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteUser(@Param("id") int id);

    @Select("SELECT * FROM users WHERE username = #{username} AND user_type = #{user_type}")
    UserEntity selectByUnameAndUtype(@Param("username") String username,@Param("user_type") int user_type);

    @Select("SELECT * FROM users WHERE username = #{username}")
    UserEntity selectByUsername(@Param("username") String username);

    @Select("SELECT * FROM users WHERE id = #{id}")
    UserEntity selectByUid(@Param("id") int id);


    @Update("UPDATE users SET pwd = #{pwd}, pwd_valid_date = #{pwd_valid_date} WHERE id = #{id}")
    int updatePwd(@Param("id") Integer id, @Param("pwd") String pwd, @Param("pwd_valid_date") long pwd_valid_date);

    @Update("UPDATE users SET pwd_error_count = #{pwd_error_count} WHERE id = #{id}")
    int updatePwdErrorCount(@Param("id") int id, @Param("pwd_error_count") int pwd_error_count);

    @Select("SELECT * FROM users WHERE teacher_id = #{teacher_id} AND user_type = 2")
    UserEntity selectTeacherById(@Param("teacher_id") int teacher_id);

    @Select("SELECT * FROM users WHERE user_type = 2")
    List<UserEntity> selectAllTeacher();


    @Select("SELECT * FROM users WHERE user_type = 3 AND student_id = #{student_id}")
    UserEntity selectStudentById(@Param("student_id") int student_id);


}
